﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

/// <summary>
/// LwUser 的摘要说明
/// </summary>
public class LwUser
{
    public const string AdminType = "admin";
    public const string StudentType = "student";
    public const string DoctorType = "doctor";
    public const string TeamType = "team";

    /***************************************************/

    public int Guid;
    public string UserType;
    public string Email;
    public string UserID;
    public string RealName;
    public string Password;
    public DateTime CreateTime;
    public string TempCode;
    public string UserCode;

    /****************************************************/
	public LwUser(int g)
	{
        string sql = "select * from Users where Guid="+g.ToString();
        this.Init(sql);
	}

    public LwUser(string uid)
    {
        string sql = "select * from Users where UserID='"+uid.Replace("'","''")+"'";
        this.Init(sql);
    }

    public void Init(string sql)
    {
        DataTable dt = Fun.CreateDt(sql);
        if (dt.Rows.Count < 1)
        {
            throw new zException(0, "");
        }
        DataRow dr = dt.Rows[0];
        this.Guid = Convert.ToInt32(dr["Guid"]);
        this.UserType = dr["UserType"].ToString().Trim().ToLower();
        this.Email = dr["Email"].ToString().Trim();
        this.UserID = dr["UserID"].ToString();
        this.RealName = dr["RealName"].ToString();
        this.Password = dr["Password"].ToString();
        this.CreateTime = Convert.ToDateTime(dr["CreateTime"]);
        this.TempCode = dr["TempCode"].ToString();
        this.UserCode = dr["UserCode"].ToString();
    }

    public virtual bool Update()
    {
        string sql = "update Users set Email='"+Fun.L(this.Email)+"',Password='"+Fun.L(this.Password)
            + "',RealName='" + Fun.L(this.RealName) + "',TempCode='" + Fun.L(this.TempCode) +
            "',UserCode='" + Fun.L(this.UserCode) + "' where Guid=" + this.Guid.ToString();
        Fun.Execute(sql);
        return true;
    }

    public int NotReadYeatNum
    {
        get
        {
            string sql = "select count(*) as n from Relationship where GuidOne="+this.Guid.ToString()+" and Relationship='"+Message.StrRecv+"'";
            return Convert.ToInt32(Fun.GetField(sql, "n"));
        }
    }

    public int RecvNum
    {
        get
        {
            string sql = "select count(*) as n from Relationship where GuidOne=" + this.Guid.ToString() + " and (Relationship='" + Message.StrRecv + "' or Relationship='"+Message.StrRead+"')";
            return Convert.ToInt32(Fun.GetField(sql, "n"));
        }
    }

    public int SentNum
    {
        get
        {
            string sql = "select count(*) as n from Relationship where GuidOne=" + this.Guid.ToString() + " and Relationship='" + Message.StrSent + "'";
            return Convert.ToInt32(Fun.GetField(sql, "n"));
        }
    }

    public DataTable RecvMes(int offset,int limit)
    {
        string sql = "select top " + limit.ToString() + " r.GuidTwo as g from Relationship r where r.GuidOne=" + this.Guid.ToString() + " and (r.Relationship='" + Message.StrRecv + "' or r.Relationship='" + Message.StrRead + "') and r.Guid not in "
            + "(select top " + offset.ToString() + " Guid from Relationship where GuidOne=" + this.Guid.ToString() + " and (Relationship='" + Message.StrRecv + "' or Relationship='" + Message.StrRead + "') order by Relationship desc,Guid desc) order by r.Relationship desc,r.Guid desc";
        DataTable dt = Fun.CreateDt(sql);
        return dt;
    }

    public DataTable SentMes(int offset, int limit)
    {
        string sql = "select top " + limit.ToString() + " r.GuidTwo as g from Relationship r where r.GuidOne=" + this.Guid.ToString() + " and r.Relationship='" + Message.StrSent + "' and r.Guid not in "
            + "(select top " + offset.ToString() + " Guid from Relationship where GuidOne=" + this.Guid.ToString() + " and Relationship='" + Message.StrSent + "' order by Guid desc) order by r.Guid desc";
        DataTable dt = Fun.CreateDt(sql);
        return dt;
    }
     
    /*****************************************************/

    public static int Login(string uid, string pwd)
    {
        string sql = "select * from Users where UserID='"+Fun.L(uid)+"'";
        DataTable dt=Fun.CreateDt(sql);
        if (dt.Rows.Count < 1)
        {
            return -1; //用户名错误
        }
        DataRow dr = dt.Rows[0];
        if (dr["password"].ToString() != pwd)
        {
            return -2; //密码错误
        }
        return Convert.ToInt32(dr["Guid"]);
    }

    public static int GetGuidByUserCode(string uc)
    {
        string sql = "select * from Users where UserCode='"+Fun.L(uc)+"'";
        DataTable dt = Fun.CreateDt(sql);
        if (dt.Rows.Count < 1)
        {
            return -1;
        }
        return Convert.ToInt32(dt.Rows[0]["Guid"]);
    }

    public static int Add(string uid,string n,string email,string ut,ref string tip)
    {
        string sql = "select count(*) as z from Users where UserID='" + Fun.L(uid) + "'";
        int c = Convert.ToInt32(Fun.GetField(sql));
        if (c > 0)
        {
            tip = "添加失败，该ID已经存在！";
            return 0;
        }
        string pwd = Fun.L(uid);
        int g = Fun.Guid();
        sql = "insert into Users(Guid,UserID,RealName,Email,Password,UserType) values("+g.ToString()+",'" + Fun.L(uid) + "','" + Fun.L(n) + "','" + Fun.L(email) + "','" + Fun.L(pwd) + "','" + ut + "')";
        Fun.Execute(sql);
        return g;
    }

    public static void Delete(int g)
    {
        Relationship.Delete(g);
        string sql = "select Guid from Message where FromGuid="+g.ToString();
        DataTable dt = Fun.CreateDt(sql);
        foreach (DataRow dr in dt.Rows)
        {
            Message.Delete(Convert.ToInt32(dr[0]));
        }
        sql = "delete from Users where Guid="+g.ToString();
        Fun.Execute(sql);
    }
}
